package com.bot.edu.informationmanage.course.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.bot.edu.applysystem.audition.entity.AuditionApplication;
import com.bot.edu.applysystem.audition.service.IAuditionApplicationService;
import com.bot.edu.informationmanage.cousesection.entity.CourseSection;
import com.bot.edu.informationmanage.cousesection.entity.vo.CourseSectionVo;
import com.bot.edu.informationmanage.cousesection.service.ICourseSectionService;
import com.bot.edu.informationmanage.majortype.entity.MajorType;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import com.bot.edu.informationmanage.course.entity.Course;
import com.bot.edu.informationmanage.course.service.ICourseService;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryRuleEnum;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;

 /**
 * @Description: 课程表
 * @Author: jeecg-boot
 * @Date:   2022-03-31
 * @Version: V1.0
 */
@Api(tags="课程表")
@RestController
@RequestMapping("/course/course")
@Slf4j
public class CourseController extends JeecgController<Course, ICourseService> {
	@Autowired
	private ICourseService courseService;

	@Autowired
	private ICourseSectionService courseSectionService;

	@Autowired
    private IAuditionApplicationService auditionApplicationService;

	/**
	 * 分页列表查询
	 *
	 * @param course
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	//@AutoLog(value = "课程表-分页列表查询")
	@ApiOperation(value="课程表-分页列表查询", notes="课程表-分页列表查询")
	@GetMapping(value = "/list")
	public Result<IPage<Course>> queryPageList(Course course,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<Course> queryWrapper = QueryGenerator.initQueryWrapper(course, req.getParameterMap());
		Page<Course> page = new Page<Course>(pageNo, pageSize);
		IPage<Course> pageList = courseService.page(page, queryWrapper);
		return Result.OK(pageList);
	}
	
	/**
	 *   添加
	 *
	 * @param course
	 * @return
	 */
	@AutoLog(value = "课程表-添加")
	@ApiOperation(value="课程表-添加", notes="课程表-添加")
	@PostMapping(value = "/add")
	public Result<String> add(@RequestBody Course course) {
		courseService.save(course);
		return Result.OK("添加成功！");
	}
	
	/**
	 *  编辑
	 *
	 * @param course
	 * @return
	 */
	@AutoLog(value = "课程表-编辑")
	@ApiOperation(value="课程表-编辑", notes="课程表-编辑")
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> edit(@RequestBody Course course) {
		courseService.updateById(course);
		return Result.OK("编辑成功!");
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "课程表-通过id删除")
	@ApiOperation(value="课程表-通过id删除", notes="课程表-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<String> delete(@RequestParam(name="id",required=true) String id) {
		courseService.removeById(id);
		return Result.OK("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "课程表-批量删除")
	@ApiOperation(value="课程表-批量删除", notes="课程表-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.courseService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	//@AutoLog(value = "课程表-通过id查询")
	@ApiOperation(value="课程表-通过id查询", notes="课程表-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<Course> queryById(@RequestParam(name="id",required=true) String id) {
		Course course = courseService.getById(id);
		if(course==null) {
			return Result.error("未找到对应数据",null);
		}
		return Result.OK(course);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param course
    */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, Course course) {
        return super.exportXls(request, course, Course.class, "课程表");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, Course.class);
    }

	 /**
	  * 查询全部课程非分页
	  */
	 @RequestMapping(value = "/getCourseSection", method = RequestMethod.GET)
	 public Result<?> getCourseSection(){
		 List<Course> list = courseService.list();
		 return Result.ok(list);
	 }
	 /**
	  * 查看课程下的所有章节
	  */
	 /*@RequestMapping(value ="/getcousesection")
	 public Result<?> getcousesection(String courseId){

		 CourseVo courseVo = new CourseVo();

         //通过课程id查到该课程
		  Course course = courseService.getById(courseId);

		  BeanUtils.copyProperties(course, courseVo);

		 //通过课程id查到该课程下的章的集合
		 List<CourseSectionVo> courseSectionList = courseService.getCourseSectionById(courseId);
		*//* BeanUtils.copyProperties(courseSectionList, courseVo);*//*
		 courseVo.setCourseSectionList(courseSectionList);

		 //获取List<CourseSection>章遍历
		 for (CourseSectionVo sectionZ : courseVo.getCourseSectionList()) {
		 	//遍历并取出sectionId
			 String sectionId = sectionZ.getSectionId();
			 //根据sectionId章获取所有节
			List<CourseSection> sectionJList = courseService.getSectionByPid(sectionId);
			//将对应的节（多）塞进对应的章（一）
			sectionZ.setSectionList(sectionJList);
		 }

		 ArrayList<CourseVo> courseVos = new ArrayList<>();
		 courseVos.add(courseVo);
		 return Result.OK(courseVos);
	 }*/

	 @GetMapping("getcousesection")
	 public Result<?> getcousesection(String courseId){
	 	 //章节的条件构造器
		 QueryWrapper<CourseSection> courseSectionWrapper = new QueryWrapper<>();
		 //通过课程的id查到该课程下的章
		 courseSectionWrapper.lambda().eq(CourseSection::getCourseId,courseId)
				 //因为章的pid为空串，所以需要让pid=""
				 .eq(CourseSection::getPid,0);
		 //创建一个章节的集合
		 ArrayList<CourseSectionVo> courseSectionVoList = new ArrayList<>();
		 //通过课程id查询到所有的章
		 List<CourseSection> courseSectionList = courseSectionService.list(courseSectionWrapper);
		 for (int i = 0; i < courseSectionList.size(); i++) {
		 	 //把节的信息塞到章的集合里面去
			 CourseSectionVo courseSectionVo = new CourseSectionVo();
			 //节的id放进章的集合里面去
			 courseSectionVo.setSectionId(courseSectionList.get(i).getSectionId());
			 //节的名字放进章的集合里面去
			 courseSectionVo.setSectionName(courseSectionList.get(i).getSectionName());
			 QueryWrapper<CourseSection> courseSectionQueryWrapper = new QueryWrapper<>();
			 //pid这一列是节的父级id所以需要让pid这一列和sectionId相关联查询
			 courseSectionQueryWrapper.lambda().eq(CourseSection::getPid,courseSectionList.get(i).getSectionId());
             //将联查条件放进sql语句中得到查询结果
			 List<CourseSection> sections = courseSectionService.list(courseSectionQueryWrapper);
			 //将查询到的结果存进节对象中
			 courseSectionVo.setSectionList(sections);
			 //将对象放进集合
			 courseSectionVoList.add(courseSectionVo);
		 }
		 return Result.ok(courseSectionVoList);
	 }

     /**
      * 根据课程id查询课程
      */
     @RequestMapping("/selectCourseByCourseId")
     public Result<?> selectCourseByCourseId(String courseId){
         Course courseServiceById = courseService.getById(courseId);
         return Result.ok(courseServiceById);
     }
     /**
      * 根据课程的专业编号找到该课程所属的专业名称
      */
     @RequestMapping("selectCourseTypeBymajorTypeId")
     public Result<?> selectCourseTypeBymajorTypeId(String MajorTypeId) {
         MajorType majorType=courseService.selectCourseTypeBymajorTypeId(MajorTypeId);
         return Result.ok(majorType);
     }


     /**
      * 获取用户试听的课程
      * @param userId
      * @return
      */
     @GetMapping("/getUserCourseList")
     public Result<?> getUserCourseList(String userId){
         QueryWrapper<AuditionApplication> auditionApplicationQueryWrapper = new QueryWrapper<>();
         auditionApplicationQueryWrapper.lambda().eq(AuditionApplication::getUserId,userId);
         List<AuditionApplication> auditionApplicationList = auditionApplicationService.list(auditionApplicationQueryWrapper);
         List courseList = new ArrayList<>();
         for (int i = 0; i < auditionApplicationList.size(); i++) {
             QueryWrapper<Course> courseQueryWrapper = new QueryWrapper<>();
             courseQueryWrapper.lambda().eq(Course::getCourseId,auditionApplicationList.get(i).getCourseId());
             Course course = courseService.getOne(courseQueryWrapper);
             courseList.add(course);
         }
         return Result.ok(courseList);
     }

 }
